//index.js

Page({
  data: {
    tabIndex: 0,
    tabnav: {
      tabnum: 5,
      tabitem: [{
          "id": 0,
          "text": "资讯"
        },
        // {
        //   "id": 1,
        //   "text": "推荐"
        // },
        {
          "id": 2,
          "text": "问答"
        },
        {
          "id": 3,
          "text": "博客"
        },
      ]
    },
    winHeight: 0,
    // 组件参数设置，传递到组件
    infomationData: {
      token: 'e6142fa662bc4bf21083870a957fbd20',
      type: 1,
      listviewHeight: 0,
      hasBanner: true,
      catalog: 4,
    },
    recommendData: {
      token: 'e6142fa662bc4bf21083870a957fbd20',
      type: 2,
      listviewHeight: 0,
    },
    qaData: {
      token: '98d04eb58a1d12b75d254deecbc83790',
      type: 3,
      listviewHeight: 0,
    },
    blogData: {
      token: 'df985be3c5d5449f8dfb47e06e098ef9',
      type: 4,
      listviewHeight: 0,
    },
  },


  onLoad: function (options) {
    var tabIndex = options.tabIndex || 0;
    var app = getApp();
    var that = this;
    var windowHeight = app.globalData.windowHeight - 30;
    that.setData({
      tabIndex: tabIndex,
      winHeight: windowHeight,
      infomationData: {
        listviewHeight: windowHeight,
      },
      recommendData: {
        listviewHeight: windowHeight,
      },
      qaData: {
        listviewHeight: windowHeight,
      },
      blogData: {
        listviewHeight: windowHeight,
      },
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var tabIndex = that.data.tabIndex;
    var shareObj = {
      title: that.getShareTitle(tabIndex), // 默认是小程序的名称(可以写slogan等)
      path: '/pages/index/index?tabIndex=' + tabIndex, // 默认是当前页面，必须是以‘/’开头的完整路径
      // imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    // 返回shareObj
    return shareObj;
  },

  onShareTimeline: function(){
    var that = this;
    var tabIndex = that.data.tabIndex;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: that.getShareTitle(tabIndex), // 默认是小程序的名称(可以写slogan等)
      query: 'tabIndex=' + that.data.tabIndex, //当前页面路径携带的参数
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;
  },

  getShareTitle: function(tabIndex){
    var userInfo = getApp().globalData.userInfo;
    var userName;
    if(userInfo){
      userName = userInfo.name || '有人';
    } else {
      userName = '有人';
    }
    var title = userName + '分享了' + this.data.tabnav.tabitem[tabIndex].text;
    return title;
  },

  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  /** 
   * 滑动切换tab 
   */
  bindChange: function (e) {
    var that = this;
    that.setData({
      tabIndex: e.detail.current
    });
  },
  /** 
   * 点击tab切换 
   */
  setTab: function (e) {
    const edata = e.currentTarget.dataset;
    this.setData({
      tabIndex: edata.tabindex,
    })
  },
})